'''
Created on 2009-11-14

@author: Administrator
'''
from google.appengine.api import users
from google.appengine.ext import db
from mod.exception import expError
from mod.member import Member
from mod.jsonout import JsonOut
from app.userfac import UserFac
from mod.blog import Blog
from mod.tagmap import TagMap
class Submit(JsonOut):
    '''
    post blog
    '''
    def get(self):
        
        try:
            user = users.get_current_user()
                
            act = self.request.get('act')
            blogid = self.request.get('blogid')
            if act == 'delete':
                if users.is_current_user_admin() == False:
                    self.redirect(users.create_login_url(self.request.uri))
                blog = db.get(blogid)
                redirect = "/?top="+blog.topblog
                blog.blogDelete()
                self.redirect(redirect)
            
            if act == 'addtag':
                tagtxt = self.request.get("tagtxt")
                blogid = self.request.get("blogid")
                
                if tagtxt != "" and blogid != "":
                    blog = db.get(blogid)
                    TagMap().addMap(tagtxt,blog)
                self.stateDirt(1,'sus')
                
                
            if act == "rebuild":
                blogs = db.GqlQuery("select * from Blog ")
                for blog in blogs :
                    blog.put()
                    self.response.out.write(blog.content+"--"+blog.topblog+"--"+str(blog.commentnum)+"<BR>")
                
        except db.datastore_errors.BadKeyError,err:
           self.displayError(err)
        
    def post(self):
        try:
            user = users.get_current_user()
            
            member = None
            if user:
                member = Member().getMember(user) 
            else:
                raise expError("not login")
            
            if member:
                content = self.request.get("content")
                topblogkey = self.request.get("topblogkey")
                noname = self.request.get("noname")
                noname = noname and int(noname) or 0
                blog = Blog(author=member,content=content,topblog=topblogkey,noname=noname)
                blog.blogSave()
                if topblogkey != "":
                    self.redirect("/?top="+topblogkey)
                else:
                    self.redirect("/")
        except expError ,e:
            self.displayError(e.value)
        except db.BadValueError,e:
            self.displayError(e)
        
    

  
        